﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace HospitalSystem
{
    public partial class ReportDetailScreening : System.Web.UI.Page
    {
        public static List<string> x4 = new List<string>();
        public static List<string> x5 = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            if (!IsPostBack)
            {
                string caseNumber = Request.QueryString["CaseNo"];
                string CID = Request.QueryString["IDStudent"];
                //อย่าลืมทำเช็คเงื่อนไข เมื่อ แปลง CaseNumber กับ CID มามั่วๆ
                if (caseNumber == "" || CID == "")
                {
                    MessageBox("Error!!!!!!!");
                }
                else
                {
                    //แสดงออกทาง Gridview โดยมี 4 แบบ คือ
                    //1. การคัดกรอง
                    BindKudKrong(caseNumber, CID);
                    //2. การวางแผน
                    BindPlanning(caseNumber, CID);
                    //3. โชว์อายุพัฒนาการของแต่ละทักษะ
                    ShowEvolutionAge(caseNumber, CID);
                    //Show Graph Timeline
                    LoadChart(this.TimeLineDataTable(caseNumber,CID), "TimeLinedata");
                }
                
            }
        }


        //คำนวณอายุพัฒนาการ
        private void ShowEvolutionAge(string caseNumber, string CID)
        {
            //List<string> tempAgeRangeforShowAtLabel = new List<string>();
            string[] tempAgeRangeforShowAtLabel = new string[10];
            List<string> ACTCodeList = new List<string>();
            List<string> AgeCodeTempList = new List<string>();
            List<string> AgeRangeTempList = new List<string>();

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();

            //Query Main Activity Code
            SqlCommand cmdACTCode = new SqlCommand("select ACTCode from Activity where SVCCode = 'A0601001'", scon);
            SqlDataReader readercmdACTCode = cmdACTCode.ExecuteReader();
            while (readercmdACTCode.Read() != false)
            {
                ACTCodeList.Add(readercmdACTCode["ACTCode"].ToString());
            }
            readercmdACTCode.Close();
            //Loop foreach 6 main Activity
            for (int i = 0; i < ACTCodeList.Count; i++)
            {
                string currentACTCode = ACTCodeList[i];
                //Query Age
                SqlCommand cmd = new SqlCommand("select AgeType.AgeCode,AgeType.AgeRange from AgeType order by AgeType.MinAgeRange", scon);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read() != false)
                {
                    AgeCodeTempList.Add(reader["AgeCode"].ToString());
                    AgeRangeTempList.Add(reader["AgeRange"].ToString());
                }
                reader.Close();
                //Looping Foreach ช่วงของอายุ ในแต่ละกิจกรรมย่อย
                for (int j = 0; j < AgeCodeTempList.Count; j++)
                {
                    List<string> tempSACTValue = new List<string>();
                    SqlCommand cmdQueryforResult = new SqlCommand("select GrowthEvaluation2.SACTValue from SubActivity2 inner join GrowthEvaluation2 on SubActivity2.SACT2Code = GrowthEvaluation2.SACT2Code inner join SubActivity on SubActivity2.SACTCode = SubActivity.SACTCode where GrowthEvaluation2.CID = @CID and GrowthEvaluation2.CaseNo = @CaseNo and SubActivity2.AgeCode = @Agecode and SubActivity.ACTCode = @CACTCode", scon);
                    cmdQueryforResult.Parameters.AddWithValue("@CID", CID);
                    cmdQueryforResult.Parameters.AddWithValue("@CaseNo", caseNumber);
                    cmdQueryforResult.Parameters.AddWithValue("@Agecode", AgeCodeTempList[j]);
                    cmdQueryforResult.Parameters.AddWithValue("@CACTCode", currentACTCode);
                    SqlDataReader readercmdQueryforResult = cmdQueryforResult.ExecuteReader();
                    while (readercmdQueryforResult.Read() != false)
                    {
                        tempSACTValue.Add(readercmdQueryforResult["SACTValue"].ToString().Trim());
                    }
                    readercmdQueryforResult.Close();
                    //ถ้าไม่มีกิจกรรมใดในช่วงอายุนี้ ก็ไม่ต้องทำอะไร
                    if (tempSACTValue.Count == 0)
                    {
                        break;
                    }
                    //ถ้ามีกิจกรรมให้เช็คค่า SACTValue
                    else
                    {
                        //ตัวแปรเก็บค่าว่ามี Value เป็น 0 ในช่วงอายุ.... ของแต่ละกิจกรรมหรือไม่
                        bool hasZerovalue = false;
                        for (int k = 0; k < tempSACTValue.Count; k++)
                        {
                            //ทำไม่ได้ บาง กิจกรรม
                            if (tempSACTValue[k] == "0")
                            {
                                hasZerovalue = true;
                                break;
                            }
                        }

                        //เช็คว่ามี ศูนย์ หรือไม่มีศูนย์
                        if (hasZerovalue == true)
                        {
                            //ถ้ามีศูนย์แสดงว่าอายุพัฒนาการก็อยู่ช่วงอายุนี้
                            tempAgeRangeforShowAtLabel[i] = AgeRangeTempList[j];
                            break;
                        }
                        else
                        {
                            //ถ้าไม่มีศูนย์เลยก็เช็คที่ช่วงอายุต่อไป
                            tempAgeRangeforShowAtLabel[i] = AgeRangeTempList[j]; 
                        }
                    }
                }
            }


            for (int i = 0; i < tempAgeRangeforShowAtLabel.Length; i++)
            {
                if (tempAgeRangeforShowAtLabel[i] == null)
                {
                    tempAgeRangeforShowAtLabel[i] = "===ไม่สามารถประเมินได้เนื่องจากไม่มีการบันทึกข้อมูลพัฒนาการ===";
                }
            }

            SmallMuscleLabel.Text = tempAgeRangeforShowAtLabel[0];
            LargeMuscleLabel.Text = tempAgeRangeforShowAtLabel[1];
            HelpMySelfLabel.Text = tempAgeRangeforShowAtLabel[2];
            LanguageSkillLabel.Text = tempAgeRangeforShowAtLabel[3];
            IQLabel.Text = tempAgeRangeforShowAtLabel[4];
            ReadyToLearnLabel.Text = tempAgeRangeforShowAtLabel[5];
        }

        private DataTable TimeLineDataTable(string caseNumber, string CID)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            
            //string[,] datatemp = new string[128,128];

            DataTable data = new DataTable();
            data.Columns.Add("Date",typeof(System.DateTime));
            data.Columns.Add("Num Activity",typeof(System.Int32));
            data.Columns.Add("Title");
            data.Columns.Add("Text");

            SqlCommand cmd = new SqlCommand("select CAST(Plannig.ExpectedDate as DATE)as Date,COUNT(Plannig.SACTCode)as SumAllACT from Plannig where Plannig.CID = @CID and Plannig.CaseNo = @CaseNo group by CAST(Plannig.ExpectedDate as DATE)", scon);
            cmd.Parameters.AddWithValue("@CID", CID);
            cmd.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader1 = cmd.ExecuteReader();
            while (reader1.Read() != false)
            {
                data.Rows.Add(reader1["Date"], reader1["SumAllACT"], reader1["Date"]);
            }
            reader1.Close();
            for (int i = 0; i < data.Rows.Count; i++)
            {
                string temp = "";
                SqlCommand cmd2 = new SqlCommand("select Activity.ACTDesc from Activity inner join Plannig on Plannig.SACTCode = Activity.ACTCode where Plannig.CID = @CID and Plannig.CaseNo = @CaseNo and CAST(Plannig.ExpectedDate as DATE) = @Date ", scon);
                cmd2.Parameters.AddWithValue("@CID", CID);
                cmd2.Parameters.AddWithValue("@CaseNo", caseNumber);
                cmd2.Parameters.AddWithValue("@Date", data.Rows[i]["Date"]);
                SqlDataReader reader2 = cmd2.ExecuteReader();
                while (reader2.Read() != false)
                {
                    temp += reader2["ACTDesc"].ToString() + ", ";
                }
                reader2.Close();
                data.Rows[i]["Text"] = temp;
            }
            scon.Close();
            return data;
        }

        private void LoadChart(DataTable table, string jsName)
        {
            var gdt = new Bortosky.Google.Visualization.GoogleDataTable(table);
            using (var mem = new System.IO.MemoryStream())
            {
                gdt.WriteJson(mem);
                mem.Position = 0;
                using (var sr = new System.IO.StreamReader(mem))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), jsName, string.Format("var {0} = {1};", jsName, sr.ReadToEnd()), true);
                    sr.Close();
                };
            }
        }

       

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        public string GetUrl()
        {
            string caseNumber = Request.QueryString["CaseNo"];
            string CID = Request.QueryString["IDStudent"];
            string url = "ReportBalancePerChild.aspx?IDStudent=" + CID + "&CaseNo=" + caseNumber;

            return url;
        }

        private void BindKudKrong(string caseNumber, string CID)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            DataTable dt = new DataTable();
            dt.Columns.Add("Evaluation");
            dt.Columns.Add("RecordStaff");
            dt.Columns.Add("EvalDateTime");

            
            string tempa = "";
            string tempb = "";
            int indexrow = 0;


            //Query in DisabilityEvaluation
            SqlCommand cmd1 = new SqlCommand("select RecorderID,EvalDateTime from DisabilityEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
            cmd1.Parameters.AddWithValue("@CID", CID);
            cmd1.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read() != false)
            {
                tempa = reader1["RecorderID"].ToString();
                tempb = reader1["EvalDateTime"].ToString();
            }
            reader1.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd2 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd2.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader2 = cmd2.ExecuteReader();
                while (reader2.Read() != false)
                {
                    tempa = reader2["StaffName"].ToString();
                }
                reader2.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "ความบกพร่อง";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }
            //========================================================
            tempa = "";
            tempb = "";
            //Query in HearEvaluation
            SqlCommand cmd3 = new SqlCommand("select RecorderID,EvalDateTime from HearEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
            cmd3.Parameters.AddWithValue("@CID", CID);
            cmd3.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader3 = cmd3.ExecuteReader();
            while (reader3.Read() != false)
            {
                tempa = reader3["RecorderID"].ToString();
                tempb = reader3["EvalDateTime"].ToString();
            }
            reader3.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd4 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd4.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader4 = cmd4.ExecuteReader();
                while (reader4.Read() != false)
                {
                    tempa = reader4["StaffName"].ToString();
                }
                reader4.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "ด้านได้ยิน";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }
            //========================================================

            tempa = "";
            tempb = "";
            //Query SightEvaluation
            SqlCommand cmd5 = new SqlCommand("select RecorderID,EvalDateTime from SightEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
            cmd5.Parameters.AddWithValue("@CID", CID);
            cmd5.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader5 = cmd5.ExecuteReader();
            while (reader5.Read() != false)
            {
                tempa = reader5["RecorderID"].ToString();
                tempb = reader5["EvalDateTime"].ToString();
            }
            reader5.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd6 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd6.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader6 = cmd6.ExecuteReader();
                while (reader6.Read() != false)
                {
                    tempa = reader6["StaffName"].ToString();
                }
                reader6.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "ด้านการมองเห็น";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }
            //========================================================
            //PhysicalEvaluation
            tempa = "";
            tempb = "";
            //Query SightEvaluation
            SqlCommand cmd7 = new SqlCommand("select RecorderID,EvalDateTime from PhysicalEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
            cmd7.Parameters.AddWithValue("@CID", CID);
            cmd7.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader7 = cmd7.ExecuteReader();
            while (reader7.Read() != false)
            {
                tempa = reader7["RecorderID"].ToString();
                tempb = reader7["EvalDateTime"].ToString();
            }
            reader7.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd8 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd8.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader8 = cmd8.ExecuteReader();
                while (reader8.Read() != false)
                {
                    tempa = reader8["StaffName"].ToString();
                }
                reader8.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "ด้านร่างกาย";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }

            //========================================================
            //FavorList
            tempa = "";
            tempb = "";
            //Query FavorList
            SqlCommand cmd9 = new SqlCommand("select RecorderID,EvalDateTime from FavorList where CID = @CID and CaseNo = @CaseNo", scon);
            cmd9.Parameters.AddWithValue("@CID", CID);
            cmd9.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader9 = cmd9.ExecuteReader();
            while (reader9.Read() != false)
            {
                tempa = reader9["RecorderID"].ToString();
                tempb = reader9["EvalDateTime"].ToString();
            }
            reader9.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd10 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd10.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader10 = cmd10.ExecuteReader();
                while (reader10.Read() != false)
                {
                    tempa = reader10["StaffName"].ToString();
                }
                reader10.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "สิ่งที่ชอบ";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }

            //========================================================
            //Tools
            tempa = "";
            tempb = "";
            //Query Tools
            SqlCommand cmd11 = new SqlCommand("select RecorderID,EvalDateTime from Tools where CID = @CID and CaseNo = @CaseNo", scon);
            cmd11.Parameters.AddWithValue("@CID", CID);
            cmd11.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader11 = cmd11.ExecuteReader();
            while (reader11.Read() != false)
            {
                tempa = reader11["RecorderID"].ToString();
                tempb = reader11["EvalDateTime"].ToString();
            }
            reader11.Close();
            if (tempa.Equals(""))
            {
                ;
            }
            else
            {
                SqlCommand cmd12 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
                cmd12.Parameters.AddWithValue("@StaffID", tempa);
                SqlDataReader reader12 = cmd12.ExecuteReader();
                while (reader12.Read() != false)
                {
                    tempa = reader12["StaffName"].ToString();
                }
                reader12.Close();
                dt.Rows.Add();
                dt.Rows[indexrow]["Evaluation"] = "บริการที่เคยได้รับ";
                dt.Rows[indexrow]["RecordStaff"] = tempa;
                dt.Rows[indexrow]["EvalDateTime"] = tempb;
                indexrow++;
            }
            scon.Close();
            //Bind DT in Gridview
            if (dt.Rows.Count == 0)
            {
                Label1.Text = "เด็กคนนี้ไม่ผ่านการคัดกรองใดๆ";
            }
            else
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        private void BindPlanning(string caseNumber, string CID)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            //Show Information Data
            List<string> x5 = new List<string>();
            SqlCommand cmd = new SqlCommand("select Co1Code,Co2Code,Co3Code,StaffID,HostCode,EvalDateTime from Evaluation where CID=@CID and CaseNo=@CaseNO", scon);
            DataTable dt = new DataTable();
            dt.Columns.Add("Co1Code");
            dt.Columns.Add("Co2Code");
            dt.Columns.Add("Co3Code");
            dt.Columns.Add("StaffID");
            dt.Columns.Add("HostCode");
            dt.Columns.Add("EvalDateTime");
            cmd.Parameters.AddWithValue("@CID", CID);
            cmd.Parameters.AddWithValue("@CaseNO", caseNumber);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                //tempa = reader["StaffName"].ToString();
                dt.Rows.Add(reader["Co1Code"], reader["Co2Code"], reader["Co3Code"], reader["StaffID"], reader["HostCode"], reader["EvalDateTime"]);
            }
            reader.Close();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //string hostCode = dt.Rows[i]["HostCode"].ToString();
                SqlCommand cmd2 = new SqlCommand("select StaffName from Staff where StaffID = @Co1Staff or StaffID = @Co2Staff or StaffID = @Co3Staff or StaffID = @StaffID", scon);
                cmd2.Parameters.AddWithValue("@Co1Staff", dt.Rows[i]["Co1Code"]);
                cmd2.Parameters.AddWithValue("@Co2Staff", dt.Rows[i]["Co2Code"]);
                cmd2.Parameters.AddWithValue("@Co3Staff", dt.Rows[i]["Co3Code"]);
                cmd2.Parameters.AddWithValue("@StaffID", dt.Rows[i]["StaffID"]);
                SqlDataReader reader2 = cmd2.ExecuteReader();
                while (reader2.Read() != false)
                {
                    x5.Add(reader2["StaffName"].ToString());
                }
                reader2.Close();
                //==========================================================
                SqlCommand cmd3 = new SqlCommand("select HostName from Host where HostID = @HostCode", scon);
                cmd3.Parameters.AddWithValue("@HostCode", dt.Rows[i]["HostCode"]);
                SqlDataReader reader3 = cmd3.ExecuteReader();
                while (reader3.Read() != false)
                {
                    Label17.Text = reader3["HostName"].ToString();
                }
                reader3.Close();
                //==========================================================
                Label18.Text = dt.Rows[i]["EvalDateTime"].ToString();

            }

            for (int i = 0; i < x5.Count; i++)
            {
                if (x5.Count == 4)
                {
                    BulletedList1.DataSource = x5;
                    BulletedList1.DataBind();
                }
                else
                {
                    Label2.Text = "สตาฟไม่ครบ 4 คน";
                }
            }

            //======================================================================
            //Show Planning Data in Gridview2
            //=============Query From ServiceProvision Table===========================
            //DataTable dx = new DataTable();
            //DataTable tempPSNumber = new DataTable();
            //DataTable temp = new DataTable();

            //dx.Columns.Add("Service");
            //dx.Columns.Add("EvalDateTime");
            //dx.Columns.Add("Activity");

            //x4.Clear();
            //x5.Clear();

            //temp.Columns.Add("ACTCode");
            //temp.Columns.Add("EvalDateTime");

            //tempPSNumber.Columns.Add("PSNumber");

            //SqlCommand cmd5 = new SqlCommand("select PSNumber from ServiceProvisionVsPerson where CID = @CID and CaseNo = @CaseNo", scon);
            //cmd5.Parameters.AddWithValue("@CID", CID);
            //cmd5.Parameters.AddWithValue("@CaseNo", caseNumber);
            //SqlDataReader reader5 = cmd5.ExecuteReader();
            //while (reader5.Read() != false)
            //{
            //    tempPSNumber.Rows.Add(reader5["PSNumber"]);
            //}
            //reader5.Close();
            //for (int i = 0; i < tempPSNumber.Rows.Count; i++)
            //{

            //    SqlCommand cmd6 = new SqlCommand("select ServiceProvisionDetails.ACTCode,ServiceProvision.PSDateTime from ServiceProvision inner join ServiceProvisionDetails on ServiceProvision.PSNumber = ServiceProvisionDetails.PSNumber where ServiceProvision.PSNumber = @PSNumber", scon);
            //    cmd6.Parameters.AddWithValue("@PSNumber", tempPSNumber.Rows[i]["PSNumber"]);
            //    SqlDataReader reader6 = cmd6.ExecuteReader();
            //    while (reader6.Read() != false)
            //    {
            //        temp.Rows.Add(reader6["ACTCode"], reader6["PSDateTime"]);
            //    }
            //    reader6.Close();
            //}

            //for (int i = 0; i < temp.Rows.Count; i++)
            //{
            //    SqlCommand cmd7 = new SqlCommand("select Service.SVCDEC,Activity.ACTDesc from Service inner join Activity on Service.SVCCode = Activity.SVCCode where Activity.ACTCode = @ACTCode", scon);
            //    cmd7.Parameters.AddWithValue("@ACTCode", temp.Rows[i]["ACTCode"]);
            //    SqlDataReader reader7 = cmd7.ExecuteReader();
            //    while (reader7.Read() != false)
            //    {
            //        dx.Rows.Add();
            //        dx.Rows[i]["Service"] = reader7["SVCDEC"].ToString();
            //        dx.Rows[i]["EvalDateTime"] = temp.Rows[i]["EvalDateTime"];
            //        dx.Rows[i]["Activity"] = reader7["ACTDesc"].ToString();
            //    }
            //    reader7.Close();
            //}
            //if (dx.Rows.Count == 0)
            //{
            //    Label2.Text = "ไม่ได้ผ่านการวางแผน";
            //}
            //else
            //{
            //    GridView2.DataSource = dx;
            //    GridView2.DataBind();
            //}
            //==========================================================================
            //======================Query From Planing Table=============================
            DataTable dataforBindGridview = new DataTable();
            dataforBindGridview.Columns.Add("Service");
            dataforBindGridview.Columns.Add("EvalDateTime");
            dataforBindGridview.Columns.Add("Activity");

            SqlCommand cmd5 = new SqlCommand("select Service.SVCDEC,Plannig.ExpectedDate,Activity.ACTDesc from Plannig inner join Activity on Plannig.SACTCode = Activity.ACTCode inner join Service on Activity.SVCCode = Service.SVCCode where Plannig.CID = @CID and Plannig.CaseNo = @CaseNo", scon);
            cmd5.Parameters.AddWithValue("@CID", CID);
            cmd5.Parameters.AddWithValue("@CaseNo", caseNumber);
            SqlDataReader reader5 = cmd5.ExecuteReader();
            while (reader5.Read() != false)
            {
                dataforBindGridview.Rows.Add(reader5["SVCDEC"],reader5["ExpectedDate"],reader5["ACTDesc"]);
            }
            reader5.Close();
            if (dataforBindGridview.Rows.Count == 0)
            {
                Label2.Text = "เคสนี้ไม่ได้มีการวางแผนกิจกรรมใดๆ";
            }
            else
            {
                GridView2.DataSource = dataforBindGridview;
                GridView2.DataBind();
            }
            scon.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string CID = Request.QueryString["IDStudent"];
            Response.Redirect("Report2.aspx?IDStudent="+CID);
        }

    }
}